package com.hdl.script.logic.view

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.hdl.script.logic.script.data.AssertScriptData
import com.hdl.script.logic.script.data.IScriptData
import com.hdl.script.model.Script
import com.hdl.script.utils.Pref

class ScriptViewModel : ViewModel() {
    private val mScriptData: MutableLiveData<MutableList<Script>> by lazy {
        MutableLiveData<MutableList<Script>>()
    }

    fun getScriptData(): MutableLiveData<MutableList<Script>> {
        return mScriptData
    }

    fun searchFile(context: Context) {
        val scriptDir = Pref.scriptDirectory()
        if (scriptDir.isNotEmpty()) {
            val scriptData: IScriptData? = if (scriptDir.startsWith("assert:")) {
                AssertScriptData()
            } else {
                null
            }
            val scripts: MutableList<Script>? = scriptData?.handler(context, scriptDir)
            if (scripts?.isNotEmpty() == true) {
                mScriptData.postValue(scripts)
            }
        }
    }


}